<template>
  <div class="prcList">
    <Loading v-if="loading"></Loading>
    <div class="info" v-else>
      <table class="listInfo" v-if="!showStart">
        <thead>
          <tr>
            <td>编号</td>
            <td>名称</td>
            <td>code码</td>
            <td>操作</td>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item,index) in tableData">
            <td>{{index+1}}</td>
            <td>{{item.name}}</td>
            <td>{{item.code}}</td>
            <td>
              <a @click='addressUp(item)'>编辑</a>
              <a @click='addressDel(item.id)'>删除</a>
            </td>
          </tr>
        </tbody>
      </table>

      <div class="itemInfo" v-if="showStart">
          <div class="formBox">
              <p class="item"><span class="name">地区名称 ：</span><input v-model="date.name" class="slong" placeholder="请输入内容"></input></p>
              <p class="item"><span class="name">地区code ：</span><input v-model="date.code" class="slong" placeholder="请输入内容"></input></p>
              <p class="item inputBox"><button @click="addressPut" class="submitInput">确定</button><button @click="empty" class="emptyInput">取消</button></p>
          </div>
      </div> 

    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import Loading from '@/components/loading'
import axios from 'axios'
import { Get, Post,Put,Delete } from '@/config/api'
export default {
  name: 'prcList',
  data() {
    return {
      loading: true,
      showStart:false,
      tableData: [],
      addressId:'',
      date:{
        name:'',
        code:''
      }
    }
  },
  mounted() {
    this.getData()
  },
  components: {
    Loading
  },
  methods: {
    //获取数据
    async getData() {
      if (this.Check()) {
        let reg=await Get('/base/region',this.$store.state.token)
        this.tableData=reg.data
        this.loading=false
      }
    },
    //编辑地区
    addressUp(e){
        this.showStart=true
        this.addressId=e.id
        this.date.name=e.name
        this.date.code=e.code
    },
    //删除地区
    async addressDel(e){
      if(this.Check()){
        let reg=await Delete('/base/region/'+e,this.$store.state.token)
        alert('删除成功')
        this.showStart=false
        this.getData()
      }
    },
    //修改地区
    async addressPut(){
        if(this.Check()){
          let data={
            "name":this.date.name,
            "code":this.date.code
          }
          let reg=await Put('/base/region/'+this.addressId,this.$store.state.token,data)
          alert('修改成功')
          this.showStart=false
          this.getData()
        }
    },
    //取消
    empty(){
      this.showStart=false
    }
  }
}

</script>
